package cst.neu.yuejian_31.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;

import cst.neu.yuejian_31.R;
import cst.neu.yuejian_31.bean.ActivityCollectBean;
import cst.neu.yuejian_31.bean.CollectActivityBean;
import cst.neu.yuejian_31.iface.ActivityCollectListener;
import cst.neu.yuejian_31.iface.CollectActivityListener;
import cst.neu.yuejian_31.model.ActivityCollectMdoel;
import cst.neu.yuejian_31.model.CollectActivityModel;
import cst.neu.yuejian_31.Common.Constant;

/**
 * Created by xxe on 2017/11/3.
 */

public class CollectActivityAdapter extends RecyclerView.Adapter implements CollectActivityListener,ActivityCollectListener {
    private Context context=null;
    private LayoutInflater layoutInflater;
    private List<CollectActivityBean> list;
    public String getUserId()
    {
        SharedPreferences sp;
        sp=context.getSharedPreferences("login", Context.MODE_PRIVATE);
        return  sp.getString("user_id","");
    }
    public CollectActivityAdapter(Context context){
        this.context=context;
        layoutInflater=LayoutInflater.from(context);

    }
    public void setList(List<CollectActivityBean> list){
        this.list=list;
        notifyDataSetChanged();

    }

//    @Override
//    public void onSuccess(ClassCollectBean classCollectBean) {
//        Toast.makeText(context, "取消收藏成功", Toast.LENGTH_SHORT).show();
//        CollectClassModel collectClassModel=new CollectClassModel();
//        collectClassModel.getClassCollect(getUserId(),this);
//
//    }




    @Override
    public void onSuccess(List<CollectActivityBean> collectActivityBeen) {
        this.list=collectActivityBeen;
        this.setList(list);
    }

    @Override
    public void onSuccess(ActivityCollectBean activityCollectBean) {
        Toast.makeText(context, "取消收藏成功", Toast.LENGTH_SHORT).show();
       CollectActivityModel collectActivityModel=new CollectActivityModel();
       collectActivityModel.getCollectActivity(getUserId(),this);
    }

    @Override
    public void onFail(String msg) {

    }

    public class   ViewHolder extends RecyclerView.ViewHolder {
        private ImageView clubPic,doCollect;
        private TextView className;
        public ViewHolder(View itemView) {
            super(itemView);
            clubPic= (ImageView) itemView.findViewById(R.id.imageView_club_cl);
            className= (TextView) itemView.findViewById(R.id.textView_clubname_cl);
            doCollect= (ImageView) itemView.findViewById(R.id.imageView_docollect);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v=layoutInflater.inflate(R.layout.item_clubcollect,parent,false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder (RecyclerView.ViewHolder holder, int position) {
        final CollectActivityBean collectActivityBean=list.get(position);
        if (collectActivityBean==null)
            return;
        ViewHolder viewHolder= (ViewHolder) holder;
        viewHolder.className.setText(collectActivityBean.getActivity_name());
        Picasso.with(context).load(Constant.ACTIVITY_URL+collectActivityBean.getActivity_pic().toString()).into(viewHolder.clubPic);
        viewHolder.doCollect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // 创建构建器
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                // 设置参数
                builder.setTitle("请确认：").setIcon(R.drawable.ic_question)
                        .setMessage("是否取消收藏")
                        .setPositiveButton("确认", new DialogInterface.OnClickListener() {// 积极

                            @Override
                            public void onClick(DialogInterface dialog,
                                                int which) {
                                ActivityCollectMdoel activityCollectMdoel =new ActivityCollectMdoel();
                                activityCollectMdoel.activityCollect(collectActivityBean.getUser_id(),collectActivityBean.getActivity_id(),CollectActivityAdapter.this);
                            }
                        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {// 消极

                    @Override
                    public void onClick(DialogInterface dialog,
                                        int which) {
                    }
                });
                builder.create().show();
            }
        });
//        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent=new Intent(context, CourseDetailActivity.class);
//                intent.putExtra("course_id",collectClassBean.getClubclass_id());
//                intent.putExtra("course_name",collectClassBean.getClubclass_name());
//                intent.putExtra("course_intro",collectClassBean.getClubclass_introduce());
//                intent.putExtra("course_pic",collectClassBean.getClubclass_pic());
//                intent.putExtra("course_money",collectClassBean.getClubclass_money());
//                intent.putExtra("course_Btime",collectClassBean.getClubclass_starttime());
//                intent.putExtra("course_Etime",collectClassBean.getClubclass_endtime());
//                intent.putExtra("course_teacher",collectClassBean.getClassteacher_name());
//                intent.putExtra("course_address",collectClassBean.getClubclass_adress());
//                context.startActivity(intent);
//            }
//        });


    }

    @Override
    public int getItemCount() {
        if (list==null)
            return 0;
        else
            return list.size();
    }
}
